/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Util;

/**
 *
 * @author Jack
 */
import Database.ConnectToDatabase;
import java.sql.*;
import java.util.Vector;

public class Order {

    private int idcustomer;
    private java.sql.Date dateorder;
    private int quantity;
    private float total;
    private int idemployee;
    private int idorder;

    /**
     * @return the idcustomer
     */
    public int getIdcustomer() {
        return idcustomer;
    }

    /**
     * @param idcustomer the idcustomer to set
     */
    public void setIdcustomer(int idcustomer) {
        this.idcustomer = idcustomer;
    }

    /**
     * @return the dateorder
     */
    public java.sql.Date getDateorder() {
        return dateorder;
    }

    /**
     * @param dateorder the dateorder to set
     */
    public void setDateorder(java.sql.Date dateorder) {
        this.dateorder = dateorder;
    }

    /**
     * @return the quantity
     */
    public int getQuantity() {
        return quantity;
    }

    /**
     * @param quantity the quantity to set
     */
    public void setQuantity(int quantity) {
        this.quantity = quantity;
    }

    public static Vector<Order> ListOrderOnID(int a) {
        Vector<Order> listidpd = new Vector<Order>();
        try {

            Connection con = ConnectToDatabase.getConnection();
            String query = "select * from Order1 where idorder=" + a + " order by idorder desc";
            Statement st = con.createStatement();
            ResultSet rs = st.executeQuery(query);
            while (rs.next()) {
                Order or = new Order();
                or.setIdorder(rs.getInt("idorder"));
                or.setDateorder(rs.getDate("dateorder"));
                or.setIdcustomer(rs.getInt("idcustomer"));
                or.setTotal(rs.getFloat("total"));
                or.setQuantity(rs.getInt("quantity"));
                or.setIdemployee(rs.getInt("idemployee"));
                listidpd.add(or);
            }
        } catch (Exception e) {
            listidpd = null;
        }
        return listidpd;
    }

    public static Vector<Order> ListOrderOnIDDispatch(int a) {
        Vector<Order> listidpd = new Vector<Order>();
        try {

            Connection con = ConnectToDatabase.getConnection();
            String query = "select order1.idorder,dateorder,idcustomer,total,quantity,idemployee from Order1,dispatchno where order1.idorder=dispatchno.idorder and  iddispatch=" + a + " order by idorder desc";
            Statement st = con.createStatement();
            ResultSet rs = st.executeQuery(query);
            while (rs.next()) {
                Order or = new Order();
                or.setIdorder(rs.getInt("idorder"));
                or.setDateorder(rs.getDate("dateorder"));
                or.setIdcustomer(rs.getInt("idcustomer"));
                or.setTotal(rs.getFloat("total"));
                or.setQuantity(rs.getInt("quantity"));
                or.setIdemployee(rs.getInt("idemployee"));
                listidpd.add(or);
            }
        } catch (Exception e) {
            listidpd = null;
        }
        return listidpd;
    }

    public static Vector<Order> ListOrder() {
        Vector<Order> listidpd = new Vector<Order>();
        try {

            Connection con = ConnectToDatabase.getConnection();
            String query = "select * from Order1" + " order by idorder desc";
            Statement st = con.createStatement();
            ResultSet rs = st.executeQuery(query);
            while (rs.next()) {
                Order or = new Order();
                or.setIdorder(rs.getInt("idorder"));
                or.setDateorder(rs.getDate("dateorder"));
                or.setIdcustomer(rs.getInt("idcustomer"));
                or.setTotal(rs.getFloat("total"));
                or.setQuantity(rs.getInt("quantity"));
                or.setIdemployee(rs.getInt("idemployee"));
                listidpd.add(or);
            }
        } catch (Exception e) {
            listidpd = null;
        }
        return listidpd;
    }

    public static Vector<Order> ListOrderOnNameCustomer(String a) {
        Vector<Order> listidpd = new Vector<Order>();
        try {

            Connection con = ConnectToDatabase.getConnection();
            String query = "select idorder,dateorder,order1.idcustomer,total,quantity,idemployee from Order1,customer where order1.idcustomer=customer.idcustomer and namecustomer like'%" + a + "%'" + " order by idorder desc";
            Statement st = con.createStatement();
            ResultSet rs = st.executeQuery(query);
            while (rs.next()) {
                Order or = new Order();
                or.setIdorder(rs.getInt("idorder"));
                or.setDateorder(rs.getDate("dateorder"));
                or.setIdcustomer(rs.getInt("idcustomer"));
                or.setTotal(rs.getFloat("total"));
                or.setQuantity(rs.getInt("quantity"));
                or.setIdemployee(rs.getInt("idemployee"));
                listidpd.add(or);
            }
        } catch (Exception e) {
            listidpd = null;
        }
        return listidpd;
    }

    public static Vector<Order> ListOrderOnIDCustomer(int a) {
        Vector<Order> listidpd = new Vector<Order>();
        try {

            Connection con = ConnectToDatabase.getConnection();
            String query = "select idorder,dateorder,order1.idcustomer,total,quantity,idemployee from Order1,customer where order1.idcustomer=customer.idcustomer and order1.idcustomer=" + a + " order by idorder desc";
            Statement st = con.createStatement();
            ResultSet rs = st.executeQuery(query);
            while (rs.next()) {
                Order or = new Order();
                or.setIdorder(rs.getInt("idorder"));
                or.setDateorder(rs.getDate("dateorder"));
                or.setIdcustomer(rs.getInt("idcustomer"));
                or.setTotal(rs.getFloat("total"));
                or.setQuantity(rs.getInt("quantity"));
                or.setIdemployee(rs.getInt("idemployee"));
                listidpd.add(or);
            }
        } catch (Exception e) {
            listidpd = null;
        }
        return listidpd;
    }

    public static String NameCustomer(int a) {
        String listidpd = new String();
        try {

            Connection con = ConnectToDatabase.getConnection();
            String query = "select namecustomer from order1,customer where order1.idcustomer=customer.idcustomer and order1.idorder=" + a;
            Statement st = con.createStatement();
            ResultSet rs = st.executeQuery(query);
            while (rs.next()) {

                listidpd = rs.getString("namecustomer");
            }
        } catch (Exception e) {
            listidpd = null;
        }
        return listidpd;
    }

    public static String NameEmployee(int a) {
        String listidpd = new String();
        try {

            Connection con = ConnectToDatabase.getConnection();
            String query = "select nameemployee from order1,employee where order1.idemployee=employee.idemployee and order1.idorder=" + a;
            Statement st = con.createStatement();
            ResultSet rs = st.executeQuery(query);
            while (rs.next()) {

                listidpd = rs.getString("nameemployee");
            }
        } catch (Exception e) {
            listidpd = null;
        }
        return listidpd;
    }

    /**
     * @return the total
     */
    public float getTotal() {
        return total;
    }

    /**
     * @param total the total to set
     */
    public void setTotal(float total) {
        this.total = total;
    }

    /**
     * @return the idemployee
     */
    public int getIdemployee() {
        return idemployee;
    }

    /**
     * @param idemployee the idemployee to set
     */
    public void setIdemployee(int idemployee) {
        this.idemployee = idemployee;
    }

    /**
     * @return the idorder
     */
    public int getIdorder() {
        return idorder;
    }

    /**
     * @param idorder the idorder to set
     */
    public void setIdorder(int idorder) {
        this.idorder = idorder;
    }

    public boolean addorder() {
        return CommonQuery.Command("insert into order1 values(" + this.idcustomer + ",'" + this.dateorder + "'," + this.total + "," + this.quantity + "," + this.idemployee + ")");
    }

    public boolean updatequantitymoney() {
        return CommonQuery.Command("update order1 set total=" + this.total + ",quantity=" + this.quantity + "where idorder=" + this.idorder);

    }

    public boolean updatecustomer() {
        return CommonQuery.Command("update order1 set idcustomer=" + this.idcustomer + "where idorder=" + this.idorder);
    }

    public boolean deleteorder() {
        return CommonQuery.Command("delete order1 where idorder=" + this.idorder);
    }

    public static Vector<Order> ListOrderTop() {
        Vector<Order> listidpd = new Vector<Order>();
        try {

            Connection con = ConnectToDatabase.getConnection();
            String query = "select top(1)idorder,idcustomer,dateorder,Total,quantity,idemployee  from order1 order by idorder desc";
            Statement st = con.createStatement();
            ResultSet rs = st.executeQuery(query);
            while (rs.next()) {
                Order or = new Order();
                or.setIdorder(rs.getInt("idorder"));
                or.setDateorder(rs.getDate("dateorder"));
                or.setIdcustomer(rs.getInt("idcustomer"));
                or.setTotal(rs.getFloat("total"));
                or.setQuantity(rs.getInt("quantity"));
                or.setIdemployee(rs.getInt("idemployee"));
                listidpd.add(or);
            }
        } catch (Exception e) {
            listidpd = null;
        }
        return listidpd;
    }

    public static Vector<Order> ListOrderToDay(java.sql.Date today) {
        Vector<Order> listidpd = new Vector<Order>();
        try {

            Connection con = ConnectToDatabase.getConnection();
            String query = "select *  from order1 where dateorder='" + today + "'" + " order by idorder desc";
            Statement st = con.createStatement();
            ResultSet rs = st.executeQuery(query);
            while (rs.next()) {
                Order or = new Order();
                or.setIdorder(rs.getInt("idorder"));
                or.setDateorder(rs.getDate("dateorder"));
                or.setIdcustomer(rs.getInt("idcustomer"));
                or.setTotal(rs.getFloat("total"));
                or.setQuantity(rs.getInt("quantity"));
                or.setIdemployee(rs.getInt("idemployee"));
                listidpd.add(or);
            }
        } catch (Exception e) {
            listidpd = null;
        }
        return listidpd;
    }
}
